{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "82594746-caa0-47f0-8ff3-5eafca45f335",
   "metadata": {},
   "source": [
    "# 操作Word文档\n",
    "\n",
    "通过 python-docx 来操作 Word\n",
    "\n",
    "## 创建docx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "78d026fd-4638-4bad-b348-44f10b84368b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from docx import Document\n",
    "from docx.shared import Cm, Pt\n",
    "\n",
    "from docx.document import Document as Doc\n",
    "\n",
    "# 创建代表Word文档的Doc对象\n",
    "document = Document()  # type: Doc\n",
    "# 添加大标题\n",
    "document.add_heading('快快乐乐学Python', 0)\n",
    "# 添加段落\n",
    "p = document.add_paragraph('Python是一门非常流行的编程语言，它')\n",
    "run = p.add_run('简单')\n",
    "run.bold = True\n",
    "run.font.size = Pt(18)\n",
    "p.add_run('而且')\n",
    "run = p.add_run('优雅')\n",
    "run.font.size = Pt(18)\n",
    "run.underline = True\n",
    "p.add_run('。')\n",
    "\n",
    "# 添加一级标题\n",
    "document.add_heading('Heading, level 1', level=1)\n",
    "# 添加带样式的段落\n",
    "document.add_paragraph('Intense quote', style='Intense Quote')\n",
    "# 添加无序列表\n",
    "document.add_paragraph(\n",
    "    'first item in unordered list', style='List Bullet'\n",
    ")\n",
    "document.add_paragraph(\n",
    "    'second item in ordered list', style='List Bullet'\n",
    ")\n",
    "# 添加有序列表\n",
    "document.add_paragraph(\n",
    "    'first item in ordered list', style='List Number'\n",
    ")\n",
    "document.add_paragraph(\n",
    "    'second item in ordered list', style='List Number'\n",
    ")\n",
    "\n",
    "# 添加图片（注意路径和图片必须要存在）\n",
    "document.add_picture('images/image.jpeg', width=Cm(5.2))\n",
    "\n",
    "# 添加分节符\n",
    "document.add_section()\n",
    "\n",
    "records = (\n",
    "    ('Tom', '男', '1995-5-5'),\n",
    "    ('Alice', '女', '1992-2-2')\n",
    ")\n",
    "# 添加表格\n",
    "table = document.add_table(rows=1, cols=3)\n",
    "table.style = 'Dark List'\n",
    "hdr_cells = table.rows[0].cells\n",
    "hdr_cells[0].text = '姓名'\n",
    "hdr_cells[1].text = '性别'\n",
    "hdr_cells[2].text = '出生日期'\n",
    "# 为表格添加行\n",
    "for name, sex, birthday in records:\n",
    "    row_cells = table.add_row().cells\n",
    "    row_cells[0].text = name\n",
    "    row_cells[1].text = sex\n",
    "    row_cells[2].text = birthday\n",
    "\n",
    "# 添加分页符\n",
    "document.add_page_break()\n",
    "\n",
    "# 保存文档\n",
    "document.save('word/demo.docx')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fead9cb-1d8e-4a0f-8f6d-4baf5b53bbd6",
   "metadata": {},
   "source": [
    "## 读取docs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6bbbadc5-4e08-465d-b5dd-27ba04649431",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1. 考试内容概述：\n",
      "1 • 考试题型包括简答题和计算题。\n",
      "2 • 简答题10道，计算题4道，总分各占50分。\n",
      "3 2. 重点复习内容：\n",
      "4 • 混凝土和钢筋共同工作原理。\n",
      "5 • 混凝土强度等级的确定方法。\n",
      "6 • 受拉钢筋公式的推导。\n",
      "7 • 受弯构件正截面受弯全过程的三个阶段。\n",
      "8 • 普通箍筋和螺旋箍筋的区别及其承载力。\n",
      "9 • 提高混凝土构件抗裂的措施。\n",
      "10 • 梁的配筋计算及受压区高度的限制。\n",
      "11 • 斜截面受剪破坏形态及无腹筋梁剪跨比的关系。\n",
      "12 • 预应力构建中的消压状态。\n",
      "13 3. 计算题重点：\n",
      "14 • 主要考察梁和柱的配筋计算。\n",
      "15 • 具体例题包括课本58页、74页、152页、153页的例题。\n",
      "16 • 计算时需注意配金率和验算条件。\n",
      "17 4. 考试注意事项：\n",
      "18 • 考试题目会简化，不需要记忆太多公式。\n",
      "19 • 钢筋间距一般取200mm，直径取8mm即可满足要求。\n",
      "20 • 不要作弊，不要留空白试卷，尽量写出公式。\n",
      "21 待办事项\n",
      "22 1. 复习重点内容：\n",
      "23 • 复习混凝土和钢筋共同工作原理、强度等级、受拉钢筋公式推导等重点内容。\n",
      "24 2. 做例题：\n",
      "25 • 复习课本58页、74页、152页、153页的例题，熟悉计算步骤和验算方法。\n",
      "26 3. 准备考试：\n",
      "27 • 确保不作弊，不留空白试卷，尽量写出公式。\n",
      "28 如果有其他问题或需要进一步的帮助，请随时联系。\n"
     ]
    }
   ],
   "source": [
    "from docx import Document\n",
    "from docx.document import Document as Doc\n",
    "\n",
    "doc = Document('word/test.docx')  # type: Doc\n",
    "for no, p in enumerate(doc.paragraphs):\n",
    "    print(no, p.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f9ab369-97f0-4cb1-8488-bf577ae748a0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
